
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_plugin_gdmap/flutter_plugin_gdmap.dart';
import 'package:flutter_plugin_gdmap/model/lat_lng.dart';
import 'amap_navi_page.dart';
import 'amap_page.dart';
import 'amap_po_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  AmapLocation().privacy();
  final result = await AmapLocation().setIOSKey('7d2a421738700781d1abb69c35b36dd9');
  print('设置key的结果: $result');
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _location = 'Unknown';
  String _distance = 'Unknown';
  String _poiString = 'Unknown';
  bool _gps;
  AmapLocation _amapLocation = AmapLocation();
  StreamSubscription<String> _locationSubscription;
  StreamSubscription<String> _poiSubscription;

  @override
  void initState() {
    if (Platform.isAndroid) {
      _amapLocation.startService;
    }
    super.initState();
    _locationSubscription = _amapLocation.onLocationChanged.listen((String location) {
      // print(location);
      if (!mounted) return;
      setState(() {
        _location = location;
      });
    });

    _poiSubscription = _amapLocation.getPoiList.listen((String location) {
      // print(location);
      if (!mounted) return;
      setState(() {
        _poiString = location;
      });
    });
  }

  @override
  void dispose() {
    if (Platform.isAndroid) {
      _amapLocation.stopService;
    }
    super.dispose();
    if (_locationSubscription != null) {
      _locationSubscription.cancel();
    }
    if (_poiSubscription != null) {
      _poiSubscription.cancel();
    }
  }

  Future<bool> inspectionGPS() async {
   bool gps = await _amapLocation.inspectionGPS;
    setState(() {
      _gps = gps;
    });
  }

  Future<void> startLocation() async {
    await _amapLocation.startLocation;
  }

  Future<void> stopLocation() async {
    await _amapLocation.stopLocation;
  }

  Future<void> getPoi() async {
    Map<String, dynamic> map = {
      "address": "",
      "page": 1
    };
    print("传送的数据：${map.toString()}");
    _amapLocation.locationSearch(map);
  }

  Future<void> getDistance() async {
    Map<String, dynamic> map = {
      "startLocation": json.encode(new LatLng(latitude: 39.990347, longitude: 116.490441)),
      "endLocation": json.encode(new LatLng(latitude: 39.989612, longitude: 116.480972))
    };
    String distance = await _amapLocation.distanceCompute(map);
    setState(() {
      _distance = distance;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: Container(
            height: double.infinity,
            width: double.infinity,
            child: SingleChildScrollView(
              padding: EdgeInsets.only(bottom: 15),
              child: Column(
                children: <Widget>[
                  new ElevatedButton(
                      onPressed: inspectionGPS, child: Text("获取GPS开启状态"),
                      style: ButtonStyle(
                        backgroundColor: Colors.red
                      )
                  ),
                  new ElevatedButton(onPressed: startLocation, child: Text("开始定位")),
                  new ElevatedButton(onPressed: stopLocation, child: Text("停止定位")),
                  new ElevatedButton(onPressed: getPoi, child: Text("获取Poi")),
                  new ElevatedButton(onPressed: getDistance, child: Text("获取两点间距离")),
                  new ElevatedButton(onPressed: goAMap, child: Text("地图")),
                  new ElevatedButton(onPressed: goAMapNavi, child: Text("导航")),
                  new ElevatedButton(onPressed: goAMapPo, child: Text("围栏")),
                  new Text('GPS开启状态: $_gps\n'),
                  new Text('动态定位: $_location\n'),
                  new Text('两点间距离: $_distance\n'),
                  new Text('poi结果: $_poiString\n'),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void goAMap(){
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return AMapPage();
    }));
  }

  void goAMapNavi(){
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return AMapNaviPage();
    }));
  }

  void goAMapPo(){
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return AMapPoPage();
    }));
  }

}
